package com.yuexia.yxim.server.processer;

import com.yuexia.yxim.common.bean.msg.ProtoMsg;
import com.yuexia.yxim.server.server.ServerSession;
import com.yuexia.yxim.server.server.SessionMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ChatRedirectProcesser
 *
 * @author wjp
 * @date 2020/6/5
 */
@Slf4j
@Service
public class ChatRedirectProcesser extends AbstractServerProcesser{

    @Override
    public ProtoMsg.HeadType type() {
        return ProtoMsg.HeadType.MESSAGE_REQUEST;
    }

    @Override
    public boolean action(ServerSession ch, ProtoMsg.Message proto) {

        //聊天处理
        ProtoMsg.MessageRequest messageRequest = proto.getMessageRequest();

        log.info("chatMsg | fro, = {}, to = {}, content = {}",
                    messageRequest.getFrom(), messageRequest.getTo(),
                    messageRequest.getContent());

        //获取接收方的chatId
        String to = messageRequest.getTo();
        List<ServerSession> sessionList = SessionMap.inst().getSessionBy(to);

        if (sessionList == null) {
            log.info("{}不在线，发送失败", to);
        }else {
            sessionList.forEach((session) -> session.writeAndFlush(proto));
        }
        return true;
    }
}
